//index.js
const Controllers = require('../controllers/Controller.js').controller;
const app = getApp();
Page({
  data: {
    tabbar: [],
    tabPage: app.globalData.tabPage,
    swiperList: [],
    banner: '../../assets/images/home/rotation chart_01@2x.png',
    wrappers: [
      {
        title: '我的客户',
        number: 0,
        key: 'shopNum',
        status: 0,
        childs: [
          {
            icon: '../../assets/images/home/home_icon_01@2x.png',
            text: '全部',
            status: 1,
            url: '/pages/Index/Customer/index?active=0',
            number: 0
          },
          {
            icon: '../../assets/images/home/home_icon_02@2x.png',
            text: '待审核',
            status: 1,
            url: '/pages/Index/Customer/index?active=1',
          },
          {
            icon: '../../assets/images/home/home_icon_03@2x.png',
            text: '审核失败',
            status: 1,
            url: '/pages/Index/Customer/index?active=2',
            number: 0,
            key: 'customerFailNum'
          },
          {
            icon: '../../assets/images/home/home_icon_04@2x.png',
            text: '邀请客户',
            status: 1,
            url: '/pages/Index/Customer/add',
          },
        ]
      },
      {
        title: '我的代理商',
        number: 0,
        key: 'agentNum',
        status: 0,
        childs: [
          {
            icon: '../../assets/images/home/home_icon_05@2x.png',
            text: '全部',
            status: 1,
            url: '/pages/Index/Agent/index?active=0',
            number: 0
          },
          {
            icon: '../../assets/images/home/home_icon_06@2x.png',
            text: '待审核',
            status: 1,
            url: '/pages/Index/Agent/index?active=1',
            number: 0
          },
          {
            icon: '../../assets/images/home/home_icon_07@2x.png',
            text: '审核失败',
            status: 1,
            url: '/pages/Index/Agent/index?active=2',
            number: 0,
            key: 'agentFailNum'
          },
          {
            icon: '../../assets/images/home/home_icon_08@2x.png',
            text: '新增代理商',
            status: 1,
            url: '/pages/Index/Agent/add',
            number: 0
          }
        ]
      },
      {
        title: '员工管理',
        number: 0,
        key: 'staffNum',
        status: 0,
        childs: [
          {
            icon: '../../assets/images/home/home_icon_09@2x.png',
            text: '我的员工',
            status: 1,
            url: '/pages/Index/Staff/index',
            number: 0
          },
          {
            icon: '../../assets/images/home/home_icon_10@2x.png',
            text: '新增员工',
            status: 1,
            url: '/pages/Index/Staff/add',
            number: 0
          }
        ]
      }
    ],
    agentIndexData: {}
  },
  onLoad(option) {
    var that = this;
    that.setData({
      tabbar: app.globalData.tabbar,
    })
  },
  onShow() {
    var that = this;
    that.listByType();
    app.judgeAgent(type => {
      console.log('judgeAgent--type==', type);
       if (type === 3) {
        that.agentIndex();
      } else if (type === 4) {
        that.staffIndex();
      } else {
        wx.showToast({
          title: '代理商员工待接入',
          icon: 'none'
        })
      }
    })
  },
  onPullDownRefresh() {
    var that = this;
    that.agentIndex();
  },
  selSwiperDetails(e) {
    console.log('selSwiperDetails--e==', e);
    
    var that = this;
    const id = e.currentTarget.dataset.id;
    app.openPage({url: '/pages/Login/agreement?type=swiperDetails&id='+id});
  },
  // 轮播图；
  listByType() {
    var that = this;
    Controllers.listByType({type: 8}).then(res => {
      const { data } = res;
      wx.setStorageSync('swiperList', data);
      const list2 = wx.getStorageSync('swiperList');
      console.log('list2==', list2);
      
      that.setData({
        swiperList: data || []
      });
      wx.hideLoading()
    }).catch(err => {
      wx.hideLoading()
    })
  },
  shopSettlement() {
    var that = this;
    // wx.showLoading();
    const params = {
      agentStaffId: 20,
      agentId: 1,
      typeId: 1,
      label: '标签',
      shopName: '门店5',
      rovinceName: '广东省',
      cityName: '广州市',
      districtName: '天河区',
      address: '门店地址',
      adminName: '负责人1',
      mobile: '13500000000',
      customer: '020-99999999',
      detail: '门店介绍',
      businessTime: '6:00-20:00',
      shopImg: 'http://111.230.226.223/upload/account/20201017/767052763245838336.png',
      idImg: 'http://111.230.226.223/upload/account/20201017/767036170763763712.png',
      idBackImg: 'http://111.230.226.223/upload/account/20201017/767036204100091904.png',
      businessImg: 'http://111.230.226.223/upload/account/20201017/767052763245838336.png',
      lng: '123.123',
      lat: '23.23',
      internalImgs: 'http://111.230.226.223/upload/account/20201017/767052763245838336.png,http://111.230.226.223/upload/account/20201017/767054095545532416.png,http://111.230.226.223/upload/account/20201017/767057376372064256.png'
    };
    Controllers.shopSettlement(params).then(res => {
      console.log('shopSettlement--res==', res);
      
      const { data } = res;
      var wrappers = that.data.wrappers;
      wrappers[0].status = 1;
      that.setData({
        wrappers,
        agentIndexData: data,
      })
      wx.hideLoading()
    }).catch(err => {
      wx.hideLoading()
    })
  },

  staffIndex() {
    var that = this;
    // wx.showLoading();
    Controllers.staffIndex({}).then(res => {
      console.log('staffIndex--res==', res);
      const { data } = res;
      wx.setStorageSync('staffIndex', data)

      var wrappers = that.data.wrappers;
      wrappers[0].status = 1;
      wrappers[0].childs[3].url = `/pages/Index/Customer/add?id=${data.id}&name=${data.name}`
      app.globalData.staffIndex = data;

      that.setData({
        wrappers,
        agentIndexData: data,
      })
      wx.hideLoading()
    }).catch(err => {
      wx.hideLoading()
    })
  },
  agentIndex() {
    var that = this;
    // wx.showLoading();
    Controllers.agentIndex({}).then(res => {
      console.log('agentIndex--res==', res);
      const { data } = res;
      wx.setStorageSync('agentIndex', data)
      var wrappers = that.data.wrappers;
      if ([0,1].includes(data.type)) {
        console.log('agentIndex--ab');
        wrappers[0].status = 1;
        wrappers[1].status = 1;
        wrappers[2].status = 1;
      } else {
        console.log('agentIndex--c');
        wrappers[0].status = 1;
        wrappers[2].status = 1;
      }
      wrappers[0].childs[3].url = `/pages/Index/Customer/add?id=${data.id}&name=${data.agentName}`
      app.globalData.agentIndexData = data;
      that.setData({
        wrappers,
        agentIndexData: data,
      })
      wx.hideLoading()
    }).catch(err => {
      console.log('agentIndex--err==', err);
      wx.hideLoading()
    })
  },
  openPage(e) {
    var that = this;
    var url = e.currentTarget.dataset.url;
    if (url) {
      app.openPage({ url });
    } else {
      wx.showToast({
        title: '未获取页面路径',
        icon: 'none'
      });
    }
  },
  tabChange(e) {
    app.tabChange(e);
  }
})